package cn.yr.oss;

import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * @ClassName : MinioOss
 * @Description : Minio
 * @Author : Lang
 * @Date 2021-10-06 11:33
 */
public class MinioOss implements Oss {

    Logger logger = LoggerFactory.getLogger(MinioOss.class);

    /**
     * AccessKey的值
     */
    private String accessKey;
    /**
     * SecretKey的值
     */
    private String secretKey;
    /**
     * 存储空间名
     */
    private String bucket;

    /**
     * 域名
     */
    private String domain;

    private MinioClient minioClient;


    public MinioOss(String accessKey, String secretKey, String bucket, String domain) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.bucket = bucket;
        //加上斜杠结尾方便统一拼接外链
        if (!Objects.isNull(domain) && !domain.endsWith("/")) {
            domain = domain + "/";
        }
        this.domain = domain;

        this.minioClient = getMinioClient();

    }

    private MinioClient getMinioClient() {
        MinioClient minioClient;
        try {
            minioClient = new MinioClient(this.domain, this.accessKey, this.secretKey);
            if (minioClient.bucketExists(this.bucket)) {
                logger.info("桶已初始化:{}", this.bucket);
            } else {
                //minioClient.makeBucket(this.bucket);
                logger.error("Minio桶不存在：{}", this.bucket);
            }
            return minioClient;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String upload(File targetFile) {
        return this.upload(targetFile, targetFile.getName());
    }

    public String upload(InputStream targetInputStream, String resourcesName, String contentType) {
        try {
            PutObjectOptions options = new PutObjectOptions(targetInputStream.available(), -1);
            options.setContentType(contentType);
            minioClient.putObject(this.bucket, resourcesName, targetInputStream, options);
            return minioClient.getObjectUrl(this.bucket, resourcesName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String upload(InputStream targetInputStream, String resourcesName) {

        try {
            PutObjectOptions options = new PutObjectOptions(targetInputStream.available(), -1);
            String contentType = URLConnection.guessContentTypeFromStream(targetInputStream);
            //该方式linux失效
            /*Path filePath = Paths.get(resourcesName);
            String contentType = Files.probeContentType(filePath);*/
            if (contentType != null && !contentType.equals("")) {
                options.setContentType(contentType);
            }
            minioClient.putObject(this.bucket, resourcesName, targetInputStream, options);
            return minioClient.getObjectUrl(this.bucket, resourcesName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String upload(byte[] data, String resourcesName) {
        ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(data);
        return upload(arrayInputStream, resourcesName);
    }

    @Override
    public String upload(String targetName, String resourcesName) {
        try {
            minioClient.putObject(this.bucket, resourcesName, targetName, null);
            return minioClient.getObjectUrl(this.bucket, resourcesName);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public String upload(File targetFile, String resourcesName) {
        try {
            FileInputStream fileInputStream = new FileInputStream(targetFile);
            return upload(fileInputStream, resourcesName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

}
